package com.jungle.bfi.service.metadata.impl;

import com.jungle.base.domain.bo.BaseUserBo;
import com.jungle.bfi.domain.bo.DataCategoryBo;
import com.jungle.bfi.entity.DataCategoryPo;
import com.jungle.base.service.AbstractService;
import com.jungle.bfi.service.metadata.DataCategoryService;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataCategoryServiceImpl extends AbstractService<DataCategoryPo, DataCategoryBo, String> implements DataCategoryService {
    @Override
    protected String getBoId(DataCategoryBo bo) {
        return bo.getId();
    }

    @Override
    protected String newPoId() {
        return Sid.next();
    }

    @Override
    protected DataCategoryPo createPo(DataCategoryBo bo, BaseUserBo userBo) {
        return mapper.map(bo, DataCategoryPo.class);
    }

    @Override
    public DataCategoryPo findByCode(String code) {
        if (code != null) {
            List<DataCategoryPo> list = repository.findAll((root, query, cb) -> cb.equal(root.get("code"), code));
            if (list.isEmpty()) {
                return null;
            } else if (list.size() == 1) {
                return list.get(0);
            }
        }
        return null;
    }
}
